<script>
	export default {
		onLaunch: function() {
			uni.hideTabBar()
			console.log('App Launch')
		},
		onShow: function() {
			console.log('App Show')
			uni.hideTabBar()
		},
		onHide: function() {
			console.log('App Hide')
		},
		methods: {
			// 时间格式化时间为：刚刚、多少分钟前、多少天前
			// stringTime 2020-09-10 20:20:20
			getDateBeforeNow(stringTime) {
				// console.log("传参未格式化", stringTime);
				stringTime = new Date(stringTime.replace(/-/g, '/'))

				// 统一单位换算
				var minute = 1000 * 60;
				var hour = minute * 60;
				var day = hour * 24;
				var week = day * 7;
				var month = day * 30;
				var year = month * 12;

				var time1 = new Date().getTime(); //当前的时间戳
				// console.log("当前时间", time1);

				// 对时间进行毫秒单位转换
				var time2 = new Date(stringTime).getTime(); //指定时间的时间戳

				// console.log("传过来的时间", time2);

				var time = time1 - time2;
				// console.log("计算后的时间", time);

				var result = null;
				if (time < 0) {
					// alert("设置的时间不能早于当前时间！");
					result = stringTime;
				} else if (time / year >= 1) {
					result = parseInt(time / year) + "年前";
				} else if (time / month >= 1) {
					result = parseInt(time / month) + "月前";
				} else if (time / week >= 1) {
					result = parseInt(time / week) + "周前";
				} else if (time / day >= 1) {
					result = parseInt(time / day) + "天前";
				} else if (time / hour >= 1) {
					result = parseInt(time / hour) + "小时前";
				} else if (time / minute >= 1) {
					result = parseInt(time / minute) + "分钟前";
				} else {
					result = "刚刚";
				}
				// console.log("格式化后的时间", result);
				return result;
			},
		}

	}
	
</script>

<style lang="scss">
	@import "uview-ui/index.scss";
	/*每个页面公共css */
</style>
